package com.jsyh.maidu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.model.Bonus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/12.
 */

public class BonusListAdapter extends RecyclerView.Adapter<BonusListAdapter.BonusHolder> {

    private List<Bonus> data;
    private Context context;

    public BonusListAdapter(List<Bonus> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public BonusHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bonus, parent, false);
        BonusHolder holder = new BonusHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BonusHolder holder, int position) {
        holder.number.setText("红包序号：" + data.get(position).getBonus_sn());
        holder.shopName.setText("发行店铺：" + data.get(position).getSupplier_name());
        holder.conditon.setText("使用条件：" + data.get(position).getType_name());
        String date = data.get(position).getUse_end_date() + "000";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long dateL = Long.valueOf(date);
        Date da = new Date(dateL);
        String endDate = sdf.format(da);
        holder.valid.setText("有效时间：" + endDate);
        String useTime = data.get(position).getUsed_time();
        StringBuilder builder = new StringBuilder();
        String money = data.get(position).getType_money();
        builder.append(money + "\n");
        if (useTime.equals("0")) {
            builder.append("未使用");
        } else {
            if (System.currentTimeMillis() > dateL) {
                builder.append("已过期");
            } else {
                builder.append("未过期");
            }
        }
        holder.amount.setText(builder.toString());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class BonusHolder extends RecyclerView.ViewHolder {

        TextView number;
        TextView shopName;
        TextView conditon;
        TextView valid;
        TextView amount;


        public BonusHolder(View itemView) {
            super(itemView);
            number = (TextView) itemView.findViewById(R.id.bonus_number);
            shopName = (TextView) itemView.findViewById(R.id.bonus_shop_name);
            conditon = (TextView) itemView.findViewById(R.id.bonus_use_condition);
            valid = (TextView) itemView.findViewById(R.id.bonus_valid_time);
            amount = (TextView) itemView.findViewById(R.id.bonus_amount);
        }
    }
}
